import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the Assistantlist page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-assistantlist',
	templateUrl: 'assistantlist.html',
})
export class AssistantlistPage {

	private selectIndex: any;

	/**
	 * 列表
	 */
	private listItems1Page: any;
	private listItems1: Array<{
		taskId: any,
		taskName: any,
		procDefId: any,
		procDefKey: any,
		procInstId: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	}>;

	/**
	 * 列表
	 */
	private listItems2Page: any;
	private listItems2: Array<{
		name: any,
		applyUserName: any,
		procDefId: any,
		procInstId: any,
		startUserId: any,
		title: any,
		taskCreateDate: any
	}>;

	/**
	 * 选择框
	 */
	private inputSearch = {
		procDefKey: new Object(),
		assignee: new Object(),
		beginDate: '',
		endDate: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public units: UnitsProvider) {
		this.selectIndex = this.navParams.get("type");
		this.listItems1 = new Array();
		this.listItems2 = new Array();

		this.listItems1Page = 1;
		this.listItems2Page = 1;
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Assistantlist');
		this.loadData(this.selectIndex);
	}

	//切换
	segmentChanged(ev: any) {
		if (ev['value'] == 0) {
			this.loadData("0");
			this.selectIndex = "0";
		} else {
			this.loadData("1");
			this.selectIndex = "1";
		}
	}

	//初始化
	loadData(index: any) {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				if (index == "0") {
					this.listItems1Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('oa', 'act/todo.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['name'];
							this.loadMakeData("0", true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else {
					this.listItems2Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('oa', 'act/historic.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['name'];
							this.loadMakeData("1", true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}
	//加载更多
	loadMoreData(index: any) {
		//异步方式
		return new Promise((success) => {
			//初始化
			this.storage.getStorage("user").then(val => {
				if (index == "0") {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page + 1,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('oa', 'act/todo.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['name'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems1Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page + 1,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('oa', 'act/historic.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['name'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems1Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}
	//共通列表
	loadMakeData(index: any, isnew: any, datas: any) {
		if (index == "0") {
			if (isnew)
				this.listItems1 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems1.push({
					taskId: item['taskId'],
					taskName: item['taskName'],
					procDefId: item['procDefId'],
					procDefKey: item['procDefKey'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['vars']['map']['title'],
					applyUserId: item['vars']['map']['applyUserId'],
					taskCreateDate: item['taskCreateDate']
				});
			}
		} else {
			if (isnew)
				this.listItems2 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems2.push({
					name: item['name'],
					applyUserName: item['applyUserName'],
					procDefId: item['procDefId'],
					procInstId: item['procInstId'],
					startUserId: item['startUserId'],
					title: item['title'],
					taskCreateDate: this.units.getIntToDateString(item['startTime'])
				});
			}
		}
	}

	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applyapplycode'] = this.inputSearch.procDefKey;
		params['applyapplymember'] = this.inputSearch.assignee;
		params['applystarttime'] = this.inputSearch.beginDate;
		params['applyendtime'] = this.inputSearch.endDate;
		data['type'] = 1; //检索类型
		data['params'] = params; //参数传递
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				this.inputSearch.procDefKey = success['applyapplycode'];
				this.inputSearch.assignee = success['applyapplymember'];
				this.inputSearch.beginDate = success['applystarttime'];
				this.inputSearch.endDate = success['applyendtime'];
				this.loadData(this.selectIndex);
			}
		});
	}

	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData(this.selectIndex).then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData(this.selectIndex).then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}
	//详情
	dodetail(item: any) {
		/***行政办公***/
		//通知公告
		if (item.procDefKey == "notifyApply") {
			this.navCtrl.push('ReadnoticedetailPage', {
				type: 6
			});
		}
		//请假申请
		if (item.procDefKey == "leave2") {
			this.navCtrl.push('AssistantqingjiaPage', {
				type: 6
			});
		}
		//出差申请
		if (item.procDefKey == "businessTrip") {
			this.navCtrl.push('AssistantchuchaiPage', {
				type: 7
			});
		}
		//项目立项申请
		if (item.procDefKey == "projectApprovalProcess") {
			this.navCtrl.push('AssistantlixiangPage', {
				type: 9
			});
		}
		//项目采购申请
		if (item.procDefKey == "projectPurchaseApply") {
			this.navCtrl.push('AssistantlixiangPage', {
				type: 10
			});
		}
	}
}